package team.paimengshop.show;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import team.paimengshop.server_socket.ActionControl_Receive;

import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;

public class ServerMainController implements Initializable
{
    @FXML
    private Button uerBtn;

    @FXML
    private Button orderBtn;

    @FXML
    private AnchorPane serverPane;

    @FXML
    private Label titleLabel;

    @FXML
    private Button shelveBtn;

    @FXML
    private Button chatBtn;





    @FXML
    void OnchatBtnClicked(ActionEvent event) //单击消息按钮，弹出消息界面
    {
        Stage chatStage = new Stage();
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("customerService-View-forServer.fxml"));

        Scene scene = null;
        try {
            scene = new Scene(fxmlLoader.load(), 1200, 700);
        } catch (IOException e) {
            e.printStackTrace();
        }

        customerServiceforServerController customerServiceforServerController = fxmlLoader.getController();
        ActionControl_Receive.setCustomerServiceforServerController(customerServiceforServerController);

        chatStage.setTitle("消息");
        chatStage.setScene(scene);
        Image logo = new Image("file:PaiMengShop/PaiMengShopServer/src/main/resources/team/paimengshop/show/image/logo.png");  //设置图标
        chatStage.getIcons().add(logo);
        chatStage.show();
    }

    @FXML
    void OnshelveBtnClicked(ActionEvent event) //单击上下架按钮，弹出上下架界面
    {
        Stage chatStage = new Stage();
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("shelves-View.fxml"));
        Scene scene = null;
        try {
            scene = new Scene(fxmlLoader.load(), 1200, 700);
        } catch (IOException e) {
            e.printStackTrace();
        }
        chatStage.setTitle("货架");
        chatStage.setScene(scene);
        Image logo = new Image("file:PaiMengShop/PaiMengShopServer/src/main/resources/team/paimengshop/show/image/logo.png");  //设置图标
        chatStage.getIcons().add(logo);
        chatStage.show();
    }

    @FXML
    void OnuerBtnClicked(ActionEvent event)     //单击用户按钮，弹出用户数据库界面
    {
        Stage userStage = new Stage();
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("user-View.fxml"));
        Scene scene = null;
        try {
            scene = new Scene(fxmlLoader.load(), 1200, 700);
        } catch (IOException e) {
            e.printStackTrace();
        }
        userStage.setTitle("用户");
        userStage.setScene(scene);
        Image logo = new Image("file:PaiMengShop/PaiMengShopServer/src/main/resources/com/example/demo24/image/logo.png");  //设置图标
        userStage.getIcons().add(logo);
        userStage.show();
    }

    @FXML
    void OnorderBtnClicked(ActionEvent event)    //单击订单按钮，弹出订单数据库界面
    {
        Stage orderStage = new Stage();
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("order-View.fxml"));
        Scene scene = null;
        try {
            scene = new Scene(fxmlLoader.load(), 1200, 700);
        } catch (IOException e) {
            e.printStackTrace();
        }
        orderStage.setTitle("订单");
        orderStage.setScene(scene);
        Image logo = new Image("file:PaiMengShop/PaiMengShopServer/src/main/resources/com/example/demo24/image/logo.png");  //设置图标
        orderStage.getIcons().add(logo);
        orderStage.show();

    }
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle)
    {
        serverPane.getStylesheets().add(getClass().getResource("All.css").toExternalForm());

        shelveBtn.getStyleClass().add("button-transparent-special-forImgBtn");
        chatBtn.getStyleClass().add("button-transparent-special-forImgBtn");
        uerBtn.getStyleClass().add("button-transparent-special-forImgBtn");
        orderBtn.getStyleClass().add("button-transparent-special-forImgBtn");

        Image img1 = new Image("file:PaiMengShop/PaiMengShopServer/src/main/resources/team/paimengshop/show/image/chat.png");
        Image img2 = new Image("file:PaiMengShop/PaiMengShopServer/src/main/resources/team/paimengshop/show/image/shelves.png");
        Image img3 = new Image("file:PaiMengShop/PaiMengShopServer/src/main/resources/team/paimengshop/show/image/user.png");
        Image img4 = new Image("file:PaiMengShop/PaiMengShopServer/src/main/resources/team/paimengshop/show/image/order.png");
        ImageView view1 = new ImageView(img1);
        ImageView view2 = new ImageView(img2);
        ImageView view3 = new ImageView(img3);
        ImageView view4 = new ImageView(img4);
        chatBtn.setGraphic(view1);
        shelveBtn.setGraphic(view2);
        uerBtn.setGraphic(view3);
        orderBtn.setGraphic(view4);

        titleLabel.getStyleClass().add("font-sign");

        int i = getTime();
        switch(i)    //问候UI设计
        {
            case 1:
            {
                titleLabel.setText("早上好！");
                break;
            }
            case 0:
            {
                titleLabel.setText("下午好！");
                break;
            }
            case -1:
            {
                titleLabel.setText("晚上好！");
                break;
            }
        }
    }

    public int getTime()  //判断当前系统时间是早上下午还是晚上，早上返回1，下午返回0，晚上返回-1
    {
        Date now =new Date();
        DateFormat df = new SimpleDateFormat("HH");
        String dateTimeString = df.format(now);
        int hour = Integer.parseInt(dateTimeString);
        if(hour > 6 && hour < 12)
        {
            return 1;
        }
        else if(hour >= 12 && hour < 18)
        {
            return 0;
        }
        else
        {
            return -1;
        }
    }
}
